package com.example.tse.rbhttpservice.service.home;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.home.model.HomeGetNewUserExclusiveRequest;
import com.example.tse.rbhttpservice.service.home.model.HomeGetNewUserExclusiveResponse;
import com.example.tse.rbhttpservice.service.home.model.HomeGetRequest;
import com.example.tse.rbhttpservice.service.home.model.HomeGetResponse;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSecondBody;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSecondRequest;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSecondResponse;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSpecials;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSpecialsRequest;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSpecialsResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;





/**
 *
 * @author wliu
 *    edi  tse
 */
public class HomeServiceImpl implements HomeService {
	private Context context;
	private static Gson gson = new Gson();


	public HomeServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 获取新用户专享
	 * @param request
	 * @param listener
	 * @return HomeGetNewUserExclusiveResponse
	 */

	    public void getNewUserExclusive(HomeGetNewUserExclusiveRequest request, final RemoteServiceListenerT< HomeGetNewUserExclusiveResponse> listener) {

        final HttpParams params = new HttpParams();



        final String url = "home/getNewUserExclusive";


            new HttpService().get(context, url, params, HomeGetNewUserExclusiveResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((HomeGetNewUserExclusiveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new HomeGetNewUserExclusiveResponse() : (HomeGetNewUserExclusiveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new HomeGetNewUserExclusiveResponse() : (HomeGetNewUserExclusiveResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




    /**
     * 获取秒杀
     * @param request
     * @param listener
     * @return HomeGetSecondResponse
     */

    public void getSecond(HomeGetSecondRequest request, final RemoteServiceListenerT< HomeGetSecondResponse> listener) {

        final HttpParams params = new HttpParams();



        final String url = "home/getSecond";



        /**
         home
         getSecond
         GetSecond
         获取秒杀
         GET
         Home
         HomeGetSecond
         home/getSecond
         array
         */

        final Type type = new TypeToken<List<HomeGetSecondBody>>() {
        }.getType();



        new HttpService().getList(context, url, params, HomeGetSecondResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            @Override
            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                List<HomeGetSecondBody> items = gson.fromJson(rudderResponse.getBody(), type);
                HomeGetSecondResponse responce = new HomeGetSecondResponse();
                responce.setGetSecond(items);
                listener.ok(responce);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);

                if (rudderResponse == null) {
                    listener.okData(new HomeGetSecondResponse());
                } else {
                    List<HomeGetSecondBody> items = gson.fromJson(rudderResponse.getBody(), type);
                    HomeGetSecondResponse responce = new HomeGetSecondResponse();
                    responce.setGetSecond(items);
                    listener.okData(rudderResponse == null ? new HomeGetSecondResponse() : responce);
                }
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);


                if (rudderResponse == null) {
                    listener.okData(new HomeGetSecondResponse(), fromWhere);
                } else {
                    List<HomeGetSecondBody> items = gson.fromJson(rudderResponse.getBody(), type);
                    HomeGetSecondResponse responce = new HomeGetSecondResponse();
                    responce.setGetSecond(items);
                    listener.okData(rudderResponse == null ? new HomeGetSecondResponse() : responce, fromWhere);
                }


            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }


        });
    }





    /**
	 * 获取热卖推荐
	 * @param request
	 * @param listener
	 * @return HomeGetSpecialsResponse
	 */

    public void getSpecials(HomeGetSpecialsRequest request, final RemoteServiceListenerT< HomeGetSpecialsResponse> listener) {

        final HttpParams params = new HttpParams();



        final String url = "home/getSpecials";



        /**
         home
         getSpecials
         GetSpecials
         获取热卖推荐
         GET
         Home
         HomeGetSpecials
         home/getSpecials
         array
         */

        final Type type = new TypeToken<List<HomeGetSpecials>>() {
        }.getType();



        new HttpService().getList(context, url, params, HomeGetSpecialsResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            @Override
            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                List<HomeGetSpecials> items = gson.fromJson(rudderResponse.getBody(), type);
                HomeGetSpecialsResponse responce = new HomeGetSpecialsResponse();
                responce.setGetSpecials(items);
                listener.okData(responce);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);

                if (rudderResponse == null) {
                    listener.okData(new HomeGetSpecialsResponse());
                } else {
                    List<HomeGetSpecials> items = gson.fromJson(rudderResponse.getBody(), type);
                    HomeGetSpecialsResponse responce = new HomeGetSpecialsResponse();
                    responce.setGetSpecials(items);
                    listener.okData(rudderResponse == null ? new HomeGetSpecialsResponse() : responce);
                }
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);


                if (rudderResponse == null) {
                    listener.okData(new HomeGetSpecialsResponse(), fromWhere);
                } else {
                    List<HomeGetSpecials> items = gson.fromJson(rudderResponse.getBody(), type);
                    HomeGetSpecialsResponse responce = new HomeGetSpecialsResponse();
                    responce.setGetSpecials(items);
                    listener.okData(rudderResponse == null ? new HomeGetSpecialsResponse() : responce, fromWhere);
                }


            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }


        });
    }




	/**
	 * 获取首页所有数据用户，包括广告、组团、秒杀、专场
	 * @param request
	 * @param listener
	 * @return HomeGetResponse
	 */

	    public void get(HomeGetRequest request, final RemoteServiceListenerT< HomeGetResponse> listener) {

        final HttpParams params = new HttpParams();



        final String url = "home/get";


            new HttpService().get(context, url, params, HomeGetResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((HomeGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new HomeGetResponse() : (HomeGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new HomeGetResponse() : (HomeGetResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




}